import { OVComponent, IObjectView, ObjectViewInfo, IObjectBlockView, objectview } from '@feng3d/objectview';
import GUI from 'lil-gui';

declare global
{
    export interface MixinsIObjectView
    {
        initview(gui: GUI): void
    }
    export interface MixinsIObjectBlockView
    {
        initview(gui: GUI): void
    }
    export interface MixinsIObjectAttributeView
    {
        initview(gui: GUI): void
    }
}

/**
 * 默认使用块的对象界面
 */
@OVComponent('OVDefault')
export class OVDefault extends GUI implements IObjectView
{
    private _space: Object;
    private _objectViewInfo: ObjectViewInfo;
    private blockViews: IObjectBlockView[];

    /**
     * 对象界面数据
     */
    constructor(objectViewInfo: ObjectViewInfo)
    {
        super();

        this._objectViewInfo = objectViewInfo;
        this._space = objectViewInfo.owner;

        this.initview(this);
    }

    initview(gui: GUI)
    {
        if (this.blockViews) return;
        this.blockViews = [];
        const objectBlockInfos = this._objectViewInfo.objectBlockInfos;
        for (let i = 0; i < objectBlockInfos.length; i++)
        {
            const displayObject = objectview.getBlockView(objectBlockInfos[i]);
            displayObject.objectView = this;
            displayObject.initview(gui);
            this.blockViews.push(displayObject);
        }
    }

    dispose()
    {
        if (!this.blockViews) return;
        for (let i = 0; i < this.blockViews.length; i++)
        {
            const displayObject = this.blockViews[i];
            displayObject.objectView = null;
        }
        this.blockViews = null;
    }

    get space(): Object
    {
        return this._space;
    }

    set space(value: Object)
    {
        this._space = value;
        this.dispose();
    }

    /**
     * 更新界面
     */
    updateView(): void
    {
        for (let i = 0; i < this.blockViews.length; i++)
        {
            this.blockViews[i].updateView();
        }
    }

    getblockView(blockName: string)
    {
        for (let i = 0; i < this.blockViews.length; i++)
        {
            if (this.blockViews[i].blockName === blockName)
            {
                return this.blockViews[i];
            }
        }

        return null;
    }

    getAttributeView(attributeName: string)
    {
        for (let i = 0; i < this.blockViews.length; i++)
        {
            const attributeView = this.blockViews[i].getAttributeView(attributeName);
            if (attributeView)
            {
                return attributeView;
            }
        }

        return null;
    }
}
